from django.core.files.storage import Storage
from django.conf import settings
from fdfs_client.client import Fdfs_client
from rest_framework import serializers

class FastDFSStorage(Storage):

    def _open(self, name, mode='rb'):

        return None

    def _save(self, name, content, max_length=None):
        conn = Fdfs_client(settings.FDFS_CONF_PATH)
        res = conn.upload_by_buffer(content.read())
        if res['Status'] != 'Upload successed':
            raise serializers.ValidationError('文件上传fdfs失败')
        return res['Remote_file_id']

    def url(self, name):

        # return 'http://192.169.110.153:8888/' + name
        return settings.FDFS_BASE_URL + name

    def exists(self, name):
        return False
